/* eslint-disable no-unused-vars */
/* eslint-disable @typescript-eslint/no-unused-vars */
const { Service } = require('egg')
const iconv = require('iconv-lite')
const cheerio = require('cheerio')
const { tableToList } = require('../utils/tools')
const { Base64 } = require('../utils/encryption')

const defaultTime = [
  '08:00~08:45', '08:55~09:40', '10:00~10:45', '10:55~11:40',
  '14:00~14:45', '14:55~15:40', '15:50~16:35', '16:45~17:30',
  '18:30~19:15', '19:25~20:10', '20:20~21:05', '21:15~22:00',
] // 记录每节课上课时间

const emptyTable = {}
defaultTime.forEach((e, i) => {
  emptyTable[`${i + 1}`] = [[], [], [], [], [], [], []]
})
module.exports = class CourseService extends Service {
  /**
   * * 学生个人课表查询
   * @param {string} term 学期
   * @return {Object} *
   */
  async table(term) {
    const Cookie = this.ctx.state.Cookie
    const apiUrl1 = `${this.ctx.state.baseUrl}/cssfjw/jw/common/showYearTerm.action`
    const res1 = await this.ctx.curl(apiUrl1, {
      method: 'POST',
      headers: {
        Cookie,
        Referer: 'http://58.20.34.197:10115/cssfjw/student/pyfa.llkc.html?menucode=S20101',
        'Content-Type': 'application/x-www-form-urlencoded',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
      },
      timeout: 10000,
    })
    const resultObj = (JSON.parse(iconv.decode(res1.data, 'gbk')))
    const xh = resultObj.userCode
    const [xn, , xqRaw] = term ? term.split('-') : ['', '', '']
    let xq = '0'
    if (term) { xq = xqRaw === '1' ? '0' : '1' }
    const params = new URLSearchParams({ xn, xq, xh }).toString() // params = 'xn=' + xn + '&xq=' + xq + '&xh=' + xh;
    const strURL = `?params=${Base64(params)}`

    const apiUrl2 = `${this.ctx.state.baseUrl}/cssfjw/student/wsxk.xskcb10319.jsp${strURL}`
    // console.log('apiUrl2', apiUrl2)
    const res2 = await this.ctx.curl(apiUrl2, {
      method: 'GET',
      headers: {
        Cookie,
        Referer: 'http://58.20.34.197:10115/cssfjw/student/xkjg.wdkb.jsp?menucode=S20301',
        'Content-Type': 'application/x-www-form-urlencoded',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
      },
      timeout: 10000,
    })
    // console.log('res.status', res2.status);
    const gbkString = iconv.decode(res2.data, 'gbk')
    if (gbkString.includes('课表暂未公布')) { return {
      table: emptyTable, defaultTime, note: '课表暂未公布', status: false,
    } }

    if (res2.status === 200) {
      try {
        const cb = (cell, rowIndex, colIndex, $) => {
          const value = cell.text().trim().replace(/\s/g, '')

          if (rowIndex > 0 && rowIndex <= 7 && colIndex > 0) {
            const courses = []
            cell.find('.td div').each((_, kbcontent) => {
              const course = []
              // eslint-disable-next-line no-shadow, consistent-return
              $(kbcontent).contents().each((_, e) => {
                // const courseValue = $(e).text().trim().replace('\t\t\t\t\t\t\t', '').replace('\n', '')
                const courseValue = $(e).text().trim().replace(/\t+/g, '').replace('\n', '')
                if (courseValue === '') return true
                if (courseValue.includes('-----')) return true
                course.push(courseValue)
              })
              if (course.length > 0)courses.push(course)
            })
            return courses
          }
          return value
        }
        // 取到横向数据
        const table1 = tableToList(gbkString, 'body table', '', cb)
        table1.splice(0, 2)
        table1.forEach((e) => { e.splice(0, 2) })
        for (let i = 0; i < table1.length; i++) { table1[i] = table1[i].map((e) => (e === '' ? [] : e)) }

        // 换为纵向数据
        // const table2 = Array.from({ length: 7 }, () => []);
        // table1.forEach(e => {
        //   for (let i = 0; i < Math.min(e.length, table2.length); i++) {
        //     table2[i].push(e[i]); }
        // });

        // 补全无课日
        const table3 = table1.map((e) => {
          if (Array.isArray(e) && e.length === 0) { return [[], [], [], [], [], []] } return e
        })

        // table3去掉[1-4]部分,
        for (let i = 0; i < table3.length; i++) {
          for (let j = 0; j < table3[i].length; j++) {
            for (let k = 0; k < table3[i][j].length; k++) {
              const e = table3[i][j][k]
              if (Array.isArray(e) && e.length > 2) {
                e[2] = e[2].replace(/\[.*?\]/g, '')
              }
            }
          }
          // 将长度不足7的[*]补全
          const orArray = table3[i]
          const neArray = orArray.slice()
          const Le = 7 - neArray.length
          if (Le > 0) {
            const padding = Array.from({ length: Le }, () => [])
            neArray.push(...padding)
            table3[i] = neArray
          }
        }
        // 键值对，数据处理
        const tables = []
        for (let i = 0; i < table3.length; i++) {
          const itable = []
          for (let j = 0; j < table3[i].length; j++) {
            const jtable = []
            for (let k = 0; k < table3[i][j].length; k++) {
              const kdataObject = {}
              // 将长度不足4的{*}补全
              const oArray = table3[i][j][k]
              const nArray = oArray.slice()
              const L = 4 - nArray.length
              if (L > 0) {
                const padding = Array.from({ length: L }, () => '')
                nArray.push(...padding)
                table3[i][j][k] = nArray
              }

              for (let l = 0; l < table3[i][j].length; l++) {
                kdataObject.N = table3[i][j][k][0]
                kdataObject.T = table3[i][j][k][1]
                kdataObject.R = table3[i][j][k][3]
                kdataObject.W = `${table3[i][j][k][2]}周`
                // kdataObject.P;
                if (table3[i][j][k][2].includes(',')) {
                  const parts = table3[i][j][k][2].split(',')
                  kdataObject.P = []
                  for (const part of parts) {
                    if (part.includes('-')) {
                      const [start, end] = part.split('-').map(Number)
                      for (let value = start; value <= end; value++) {
                        kdataObject.P.push(value.toString())
                      }
                    } else {
                      kdataObject.P.push(part)
                    }
                  }
                } else {
                  kdataObject.P = [table3[i][j][k][2]]
                }
                // 假设只包含'-'
                const rangeStr = kdataObject.P[0]
                if (rangeStr.includes('-')) {
                  const [start, end] = rangeStr.split('-').map(Number)
                  kdataObject.P = []
                  for (let q = start; q <= end; q++) {
                    kdataObject.P.push(String(q))
                  }
                }
              }
              jtable.push(kdataObject)
            }
            if (j === 9) { console.log('table3[i][j]', table3[i][j]) }
            itable.push(jtable)
          }
          if (i === 9) { console.log('table3[i]', table3[i]) }
          tables.push(itable)
        }

        // 补全课时
        // if (tables.length < 6) {
        //   tables.push([[], [], [], [], [], [], []])
        // }
        if (6 - tables.length > 0) {
          for (let i = 0; i < 6 - tables.length + 1; i++) {
            tables.push([[], [], [], [], [], [], []]) // 添加一组七个空数组
          }
        }

        // 补全1-12键值
        const table4 = {}
        for (let i = 0; i < 13; i++) {
          table4[i] = tables[Math.floor((i - 1) / 2)]
        }

        const $ = cheerio.load(gbkString)
        const nt = $('body div:nth-child(5)').text().replace(/\s/g, '').replace(/注/g, ';注').replace(';', '')

        return {
          table: table4, time: defaultTime, note: nt, status: true,
        }
      }
      catch (err) {
        console.log('err', err)
        throw err
      }
    }
    // return { table: [], time, note: '', status: false };
    return {
      table: emptyTable, time: defaultTime, note: '', status: false,
    }
  }

  /**
   * * 班级课表查询
   * @param {string} term 学期
   * @param {string} yx 院系
   * @param {string} nj 年级
   * @param {string} zy 专业
   * @param {string} name 班级名称
   * @return {Array} *
   */
  async Ctable(term, yx, nj, zy, name) {
    // term = '2023-2024-1'// term前端传值
    const [xn, , xqRaw] = term ? term.split('-') : ['', '', '']
    // const xq = xqRaw === '1' ? '0' : '1'
    let xq = '0'
    if (term) { xq = xqRaw === '1' ? '0' : '1' }
    const Cookie = this.ctx.state.Cookie
    const apiUrl = `${this.ctx.state.baseUrl}/cssfjw/kbbp/dykb.GS1.jsp?kblx=bjkb`
    const res = await this.ctx.curl(apiUrl, {
      method: 'POST',
      data: {
        hidBJDM: '2019720974', // 班级编号
        hidCXLX: 'fbj',
        xn,
        xq_m: xq,
      },
      headers: {
        Cookie,
        Referer: 'http://58.20.34.197:10115/cssfjw/kbbp/dykb.bjkb.html?menucode=SB03',
        'Content-Type': 'application/x-www-form-urlencoded',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
      },
      timeout: 10000,
    })
    const gbkString = iconv.decode(res.data, 'gbk')
    console.log('gbkString', gbkString)
    if (res.status === 200) {
      try {
        const cb = (cell, rowIndex, colIndex, $) => {
          const value = cell.text().trim().replace(/\s/g, '')

          if (rowIndex > 0 && rowIndex <= 7 && colIndex > 0) {
            const courses = []
            cell.find('.td div').each((_, kbcontent) => {
              const course = []
              // eslint-disable-next-line no-shadow, consistent-return
              $(kbcontent).contents().each((_, e) => {
                const courseValue = $(e).text().trim().replace('\t\t\t\t\t\t\t', '').replace('\n', '')
                if (courseValue === '') return true
                if (courseValue.includes('-----')) return true
                course.push(courseValue)
              })
              if (course.length > 0)courses.push(course)
            })
            return courses
          }
          return value
        }
        let table1 = tableToList(gbkString, 'body .table', '', cb)
        table1.splice(0, 1)
        table1.forEach((e) => { e.splice(0, 2) })
        // 分割数据预留处理
        for (let i = 0; i < table1.length; i++) {
          for (let j = 0; j < table1[i].length; j++) {
            for (let k = 0; k < table1[i][j].length; k++) {
              const valuesArray = Object.values(table1[i][j][k])
              if (valuesArray.length > 1) {
                const vl = valuesArray.length
                const tvl = []
                for (let l = 0; l < vl; l++) {
                  tvl.push([valuesArray[l]])
                }
                table1[i][j] = tvl
              }
              if (table1[i][j][k].length === 1) {
                const table1Entry = table1[i][j][k][0].replace(/\s+/g, '$')
                table1[i][j][k] = table1Entry.split('$')
              }
              if (table1[i][j][k].length === 7) {
                table1[i][j][k][4] += table1[i][j][k][5]
                table1[i][j][k].splice(5, 1)
              }
              if (table1[i][j][k].length === 5) {
                table1[i][j][k].splice(3, 2)
                table1[i][j][k].push('')
              }
              if (table1[i][j][k].length === 6) {
                table1[i][j][k].splice(3, 1)
                table1[i][j][k].splice(4, 1)
              }
              table1[i][j][k][2] = table1[i][j][k][2].replace(/\[([^\]]*)\]周/g, (match, capture) => capture)
            }
          }
        }
        // 将table长度不足6的[*]补全
        const newArray = table1.slice()
        const Le = 6 - newArray.length
        if (Le > 0) {
          const padding = Array.from({ length: Le }, () => [[], [], [], [], [], []])
          newArray.push(...padding)
          table1 = newArray
        }
        // 补全无课日
        const table3 = table1.map((e) => {
          if (Array.isArray(e) && e.length === 0) { return [[], [], [], [], [], []] } return e })
        // table3去掉[1-4]部分,
        for (let i = 0; i < table3.length; i++) {
          // for (let j = 0; j < table3[i].length; j++) {
          //   for (let k = 0; k < table3[i][j].length; k++) {
          //     const e = table3[i][j][k];
          //     if (Array.isArray(e) && e.length > 2) {
          //       e[2] = e[2].replace(/\[.*?\]/g, '');
          //     }
          //   }
          // }

          // 将长度不足7的[*]补全
          const orArray = table3[i]
          const neArray = orArray.slice()
          const Le2 = 7 - neArray.length
          if (Le2 > 0) {
            const padding = Array.from({ length: Le2 }, () => [])
            neArray.push(...padding)
            table3[i] = neArray
          }
        }
        // 键值对，数据处理
        const tables = []
        for (let i = 0; i < table3.length; i++) {
          const itable = []
          for (let j = 0; j < table3[i].length; j++) {
            const jtable = []
            for (let k = 0; k < table3[i][j].length; k++) {
              const kdataObject = {}
              // 将长度不足4的{*}补全
              // const oArray = table3[i][j][k];
              // const nArray = oArray.slice();
              // const L = 4 - nArray.length;
              // if (L > 0) {
              //   const padding = Array.from({ length: L }, () => '');
              //   nArray.push(...padding);
              //   table3[i][j][k] = nArray;
              // }

              for (let l = 0; l < table3[i][j].length; l++) {
                kdataObject.N = table3[i][j][k][0]
                kdataObject.T = table3[i][j][k][1]
                kdataObject.R = table3[i][j][k][3]
                kdataObject.W = `${table3[i][j][k][2]}周`
                // kdataObject.P;
                if (table3[i][j][k][2].includes(',')) {
                  const parts = table3[i][j][k][2].split(',')
                  kdataObject.P = []
                  for (const part of parts) {
                    if (part.includes('-')) {
                      const [start, end] = part.split('-').map(Number)
                      for (let value = start; value <= end; value++) {
                        kdataObject.P.push(value.toString())
                      }
                    } else {
                      kdataObject.P.push(part)
                    }
                  }
                } else {
                  kdataObject.P = [table3[i][j][k][2]]
                }
                // 假设只包含'-'
                const rangeStr = kdataObject.P[0]
                if (rangeStr.includes('-')) {
                  const [start, end] = rangeStr.split('-').map(Number)
                  kdataObject.P = []
                  for (let q = start; q <= end; q++) {
                    kdataObject.P.push(String(q))
                  }
                }
              }
              jtable.push(kdataObject)
            }
            if (j === 9) { console.log('table3[i][j]', table3[i][j]) }
            itable.push(jtable)
          }
          if (i === 9) { console.log('table3[i]', table3[i]) }
          tables.push(itable)
        }
        // 补全1-12键值
        const table4 = {}
        for (let i = 0; i < 13; i++) {
          table4[i] = tables[Math.floor((i - 1) / 2)]
        }

        // const time = [
        //   '08:00~08:45', '08:55~09:40', '10:00~10:45', '10:55~11:40',
        //   '14:00~14:45', '14:55~15:40', '15:50~16:35', '16:45~17:30',
        //   '18:30~19:15', '19:25~20:10', '20:20~21:05', '21:15~22:00',
        // ]; // 记录每节课上课时间

        // return { table: table4, time, note: '' };
        return table4
      } catch (err) {
        console.log('err', err)
        throw err
      }
    }
    // return { table: [], time: [], note: [] };
    return []
  }

  /**
   * * 教师课表查询
   * @param {string} term 学期
   * @param {string} yx 院系
   * @param {string} name 教师姓名
   * @return {Array} *
   */
  async Ttable(term, yx, name) {
    // term = '2023-2024-1'// term前端传值
    const [xn, , xqRaw] = term ? term.split('-') : ['', '', '']
    // const xq = xqRaw === '1' ? '0' : '1'
    let xq = '0'
    if (term) { xq = xqRaw === '1' ? '0' : '1' }
    const Cookie = this.ctx.state.Cookie
    const apiUrl = `${this.ctx.state.baseUrl}/cssfjw/kbbp/dykb.GS1.jsp?kblx=jskb`
    const res = await this.ctx.curl(apiUrl, {
      method: 'POST',
      data: {
        xn,
        xq_m: xq,
        hidJSDM: '0000861', // 教师编号
        hidCXLX: 'fjs',

      },
      headers: {
        Cookie,
        Referer: 'http://58.20.34.197:10115/cssfjw/kbbp/dykb.jskb.html?menucode=SB02',
        'Content-Type': 'application/x-www-form-urlencoded',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
      },
      timeout: 10000,
    })
    const gbkString = iconv.decode(res.data, 'gbk')
    if (res.status === 200) {
      try {
        const cb = (cell, rowIndex, colIndex, $) => {
          const value = cell.text().trim().replace(/\s/g, '')

          if (rowIndex > 0 && rowIndex <= 7 && colIndex > 0) {
            const courses = []
            cell.find('.td div').each((_, kbcontent) => {
              const course = []
              // eslint-disable-next-line no-shadow, consistent-return
              $(kbcontent).contents().each((_, e) => {
                const courseValue = $(e).text().trim().replace('\t\t\t\t\t\t\t', '').replace('\n', '')
                if (courseValue === '') return true
                if (courseValue.includes('-----')) return true
                course.push(courseValue)
              })
              if (course.length > 0)courses.push(course)
            })
            return courses
          }
          return value
        }
        // 取到横向数据
        let table1 = tableToList(gbkString, 'body .table', '', cb)
        table1.splice(0, 1)
        table1.forEach((e) => { e.splice(0, 2) })
        // 分割数据预留处理
        for (let i = 0; i < table1.length; i++) {
          for (let j = 0; j < table1[i].length; j++) {
            for (let k = 0; k < table1[i][j].length; k++) {
              const valuesArray = Object.values(table1[i][j][k])
              if (valuesArray.length > 1) {
                const vl = valuesArray.length
                const tvl = []
                for (let l = 0; l < vl; l++) {
                  tvl.push([valuesArray[l]])
                }
                table1[i][j] = tvl
              }
              if (table1[i][j][k].length === 1) {
                const table1Entry = table1[i][j][k][0].replace(/\s+/g, '$')
                table1[i][j][k] = table1Entry.split('$')
              }
              if (table1[i][j][k].length === 8) {
                table1[i][j][k][5] += table1[i][j][k][6]
                table1[i][j][k].splice(6, 1)
              }
              if (table1[i][j][k].length === 6) {
                const newArray = table1[i][j][k].slice(0, 5)
                newArray.push('')

                if (table1[i][j][k].length > 5) {
                  newArray.push(...table1[i][j][k].slice(5))
                }
                table1[i][j][k] = newArray
              }
              if (table1[i][j][k].length === 7) {
                table1[i][j][k].splice(1, 1)
                table1[i][j][k].splice(2, 2)
              }
              table1[i][j][k][1] = table1[i][j][k][1].replace(/\[([^\]]*)\]周/g, (match, capture) => capture)
            }
          }
        }
        // 将table长度不足6的[*]补全
        const newArray = table1.slice()
        const Le = 6 - newArray.length
        if (Le > 0) {
          const padding = Array.from({ length: Le }, () => [[], [], [], [], [], []])
          newArray.push(...padding)
          table1 = newArray
        }
        // 补全无课日
        const table3 = table1.map((e) => {
          if (Array.isArray(e) && e.length === 0) { return [[], [], [], [], [], []] } return e })
        for (let i = 0; i < table3.length; i++) {
          // 将长度不足7的[*]补全
          const orArray = table3[i]
          const neArray = orArray.slice()
          const Le2 = 7 - neArray.length
          if (Le2 > 0) {
            const padding = Array.from({ length: Le2 }, () => [])
            neArray.push(...padding)
            table3[i] = neArray
          }
        }
        // 键值对，数据处理
        const tables = []
        for (let i = 0; i < table3.length; i++) {
          const itable = []
          for (let j = 0; j < table3[i].length; j++) {
            const jtable = []
            for (let k = 0; k < table3[i][j].length; k++) {
              const kdataObject = {}
              for (let l = 0; l < table3[i][j].length; l++) {
                kdataObject.N = table3[i][j][k][0]
                // kdataObject.P;
                if (table3[i][j][k][1].includes(',')) {
                  const parts = table3[i][j][k][1].split(',')
                  kdataObject.P = []
                  for (const part of parts) {
                    if (part.includes('-')) {
                      const [start, end] = part.split('-').map(Number)
                      for (let value = start; value <= end; value++) {
                        kdataObject.P.push(value.toString())
                      }
                    } else {
                      kdataObject.P.push(part)
                    }
                  }
                } else {
                  kdataObject.P = [table3[i][j][k][1]]
                }
                // 假设只包含'-'
                const rangeStr = kdataObject.P[0]
                if (rangeStr.includes('-')) {
                  const [start, end] = rangeStr.split('-').map(Number)
                  kdataObject.P = []
                  for (let q = start; q <= end; q++) {
                    kdataObject.P.push(String(q))
                  }
                }
                kdataObject.R = table3[i][j][k][2]
                kdataObject.W = `${table3[i][j][k][1]}周`
                kdataObject.C = table3[i][j][k][3]
              }
              jtable.push(kdataObject)
            }
            if (j === 9) { console.log('table3[i][j]', table3[i][j]) }
            itable.push(jtable)
          }
          if (i === 9) { console.log('table3[i]', table3[i]) }
          tables.push(itable)
        }
        // 补全1-12键值
        const table4 = {}
        for (let i = 0; i < 13; i++) {
          table4[i] = tables[Math.floor((i - 1) / 2)]
        }

        return table4
      } catch (err) {
        console.log('err', err)
        throw err
      }
    }
    return []
  }

  /**
   * * 教室课表查询
   * @param {string} term 学期
   * @param {string} building 教学楼
   * @param {string} name 教室名称
   * @return {Array} *
   */
  async Rtable(term, building, name) {
    // term = '2023-2024-2'// term前端传值
    const [xn, , xqRaw] = term ? term.split('-') : ['', '', '']
    // const xq = xqRaw === '1' ? '0' : '1'
    let xq = '0'
    if (term) { xq = xqRaw === '1' ? '0' : '1' }
    const Cookie = this.ctx.state.Cookie
    const apiUrl = `${this.ctx.state.baseUrl}/cssfjw/kbbp/dykb.GS1.jsp?kblx=jsikb`
    const res = await this.ctx.curl(apiUrl, {
      method: 'POST',
      data: {
        hidFJBH: '1030201', // 教室编号
        hidCXLX: 'fjsi',
        xn,
        xq_m: xq,
      },
      headers: {
        Cookie,
        Referer: 'http://58.20.34.197:10115/cssfjw/kbbp/dykb.jsikb.html?menucode=SB04',
        'Content-Type': 'application/x-www-form-urlencoded',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
      },
      timeout: 20000,
    })
    const gbkString = iconv.decode(res.data, 'gbk')

    if (res.status === 200) {
      try {
        const cb = (cell, rowIndex, colIndex, $) => {
          const value = cell.text().trim().replace(/\s/g, '')

          if (rowIndex > 0 && rowIndex <= 7 && colIndex > 0) {
            const courses = []
            cell.find('.td div').each((_, kbcontent) => {
              const course = []
              // eslint-disable-next-line no-shadow, consistent-return
              $(kbcontent).contents().each((_, e) => {
                const courseValue = $(e).text().trim().replace('\t\t\t\t\t\t\t', '').replace('\n', '')
                if (courseValue === '') return true
                if (courseValue.includes('-----')) return true
                course.push(courseValue)
              })
              if (course.length > 0)courses.push(course)
            })
            return courses
          }
          return value
        }
        // 取到横向数据
        let table1 = tableToList(gbkString, 'body .table', '', cb)
        table1.splice(0, 1)
        table1.forEach((e) => { e.splice(0, 2) })
        // 分割数据预留处理
        for (let i = 0; i < table1.length; i++) {
          for (let j = 0; j < table1[i].length; j++) {
            for (let k = 0; k < table1[i][j].length; k++) {
              const valuesArray = Object.values(table1[i][j][k])
              if (valuesArray.length > 1) {
                const vl = valuesArray.length
                const tvl = []
                for (let l = 0; l < vl; l++) {
                  tvl.push([valuesArray[l]])
                }
                table1[i][j] = tvl
              }
              if (table1[i][j][k].length === 1) {
                const table1Entry = table1[i][j][k][0].replace(/\s+/g, '$')
                table1[i][j][k] = table1Entry.split('$')
              }
              if (table1[i][j][k].length === 7) {
                table1[i][j][k].splice(1, 1)
                table1[i][j][k].splice(3, 1)
                table1[i][j][k].splice(4, 1)
              }
              table1[i][j][k][2] = table1[i][j][k][2].replace(/\[([^\]]*)\]周/g, (match, capture) => capture)
            }
          }
        }
        // 将table长度不足6的[*]补全
        const newArray = table1.slice()
        const Le = 6 - newArray.length
        if (Le > 0) {
          const padding = Array.from({ length: Le }, () => [[], [], [], [], [], []])
          newArray.push(...padding)
          table1 = newArray
        }
        // 补全无课日
        const table3 = table1.map((e) => {
          if (Array.isArray(e) && e.length === 0) { return [[], [], [], [], [], []] } return e })
        for (let i = 0; i < table3.length; i++) {
          // 将长度不足7的[*]补全
          const orArray = table3[i]
          const neArray = orArray.slice()
          const Le2 = 7 - neArray.length
          if (Le2 > 0) {
            const padding = Array.from({ length: Le2 }, () => [])
            neArray.push(...padding)
            table3[i] = neArray
          }
        }
        // 键值对，数据处理
        const tables = []
        for (let i = 0; i < table3.length; i++) {
          const itable = []
          for (let j = 0; j < table3[i].length; j++) {
            const jtable = []
            for (let k = 0; k < table3[i][j].length; k++) {
              const kdataObject = {}
              for (let l = 0; l < table3[i][j].length; l++) {
                kdataObject.N = table3[i][j][k][0]
                // kdataObject.P;
                if (table3[i][j][k][2].includes(',')) {
                  const parts = table3[i][j][k][2].split(',')
                  kdataObject.P = []
                  for (const part of parts) {
                    if (part.includes('-')) {
                      const [start, end] = part.split('-').map(Number)
                      for (let value = start; value <= end; value++) {
                        kdataObject.P.push(value.toString())
                      }
                    } else {
                      kdataObject.P.push(part)
                    }
                  }
                } else {
                  kdataObject.P = [table3[i][j][k][2]]
                }
                // 假设只包含'-'
                const rangeStr = kdataObject.P[0]
                if (rangeStr.includes('-')) {
                  const [start, end] = rangeStr.split('-').map(Number)
                  kdataObject.P = []
                  for (let q = start; q <= end; q++) {
                    kdataObject.P.push(String(q))
                  }
                }
                kdataObject.T = table3[i][j][k][1]
                kdataObject.W = `${table3[i][j][k][2]}周`
                kdataObject.C = table3[i][j][k][3]
              }
              jtable.push(kdataObject)
            }
            if (j === 9) { console.log('table3[i][j]', table3[i][j]) }
            itable.push(jtable)
          }
          if (i === 9) { console.log('table3[i]', table3[i]) }
          tables.push(itable)
        }
        // 补全1-12键值
        const table4 = {}
        for (let i = 0; i < 13; i++) {
          table4[i] = tables[Math.floor((i - 1) / 2)]
        }

        return table4
      } catch (err) {
        console.log('err', err)
        throw err
      }
    }
    return []
  }
}
